package cn.ee.dfs.monitor.controller;

import cn.ee.dfs.common.annotation.Log;
import cn.ee.dfs.common.utils.DfsUtil;
import cn.ee.dfs.common.entity.DfsConstant;
import cn.ee.dfs.common.controller.BaseController;
import cn.ee.dfs.common.entity.DfsResponse;
import cn.ee.dfs.common.entity.QueryRequest;
import cn.ee.dfs.common.exception.DfsException;
import cn.ee.dfs.monitor.entity.ApiLog;
import cn.ee.dfs.monitor.service.IApiLogService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 操作日志表 Controller
 *
 * @author TWJ
 * @date 2019-08-20 19:05:47
 */
@Slf4j
@Validated
@Controller
public class ApiLogController extends BaseController {

    @Autowired
    private IApiLogService apiLogService;

    @GetMapping(DfsConstant.VIEW_PREFIX + "apiLog")
    private String apiLogIndex(){
        return DfsUtil.view("apiLog/apiLog");
    }

    @GetMapping("apiLog")
    @ResponseBody
    @RequiresPermissions("apiLog:list")
    public DfsResponse getAllApiLogs(ApiLog apiLog) {
        return new DfsResponse().success().data(apiLogService.findApiLogs(apiLog));
    }

    @GetMapping("apiLog/list")
    @ResponseBody
    @RequiresPermissions("apiLog:list")
    public DfsResponse apiLogList(QueryRequest request, ApiLog apiLog) {
        Map<String, Object> dataTable = getDataTable(this.apiLogService.findApiLogs(request, apiLog));
        return new DfsResponse().success().data(dataTable);
    }

    @Log("新增ApiLog")
    @PostMapping("apiLog")
    @ResponseBody
    @RequiresPermissions("apiLog:add")
    public DfsResponse addApiLog(@Valid ApiLog apiLog) throws DfsException {
        try {
//            this.apiLogService.saveLog(apiLog);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "新增ApiLog失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @Log("删除ApiLog")
    @GetMapping("apiLog/delete")
    @ResponseBody
    @RequiresPermissions("apiLog:delete")
    public DfsResponse deleteApiLog(ApiLog apiLog) throws DfsException {
        try {
            this.apiLogService.deleteApiLog(apiLog);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "删除ApiLog失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @Log("修改ApiLog")
    @PostMapping("apiLog/update")
    @ResponseBody
    @RequiresPermissions("apiLog:update")
    public DfsResponse updateApiLog(ApiLog apiLog) throws DfsException {
        try {
            this.apiLogService.updateApiLog(apiLog);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "修改ApiLog失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @PostMapping("apiLog/excel")
    @ResponseBody
    @RequiresPermissions("apiLog:export")
    public void export(QueryRequest queryRequest, ApiLog apiLog, HttpServletResponse response) throws DfsException {
        try {
            List<ApiLog> apiLogs = this.apiLogService.findApiLogs(queryRequest, apiLog).getRecords();
            ExcelKit.$Export(ApiLog.class, response).downXlsx(apiLogs, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }
}
